package com.d1711.zhanggm.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.d1711.zhanggm.springboot.entity.Area;
import com.d1711.zhanggm.springboot.entity.Student;
import com.d1711.zhanggm.springboot.entity.StudentVo;
import com.d1711.zhanggm.springboot.entity.SudentHobbyRelation;
import com.d1711.zhanggm.springboot.service.IAreaService;
import com.d1711.zhanggm.springboot.service.IStudentService;
import com.d1711.zhanggm.springboot.service.ISudentHobbyRelationService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 学生表 前端控制器
 * </p>
 *
 * @author 张国明
 * @since 2020-05-27
 */
@RestController
@RequestMapping("/student")
@CrossOrigin
public class StudentController {
    @Autowired
    private IStudentService iStudentService;
    @Autowired
    private ISudentHobbyRelationService iSudentHobbyRelationService;
    @Autowired
    private IAreaService iAreaService;

    /**
     * Json列表页接口
     * @param studentVo
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("getPageInfo")
    public PageInfo<StudentVo> getPageInfo(StudentVo studentVo,
                                      @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        return iStudentService.getPageInfo(studentVo,pageNum,pageSize);
    }

    /**
     * 批量删除
     * @param studentVo
     * @return
     */
    @PostMapping("batchDel")
    public boolean batchDel(@RequestBody StudentVo studentVo){
        boolean result = iStudentService.removeByIds(studentVo.getIdList());
        return result;
    }

    /**
     * 保存或修改数据接口
     * @param studentVo
     * @return
     */
    @PostMapping("save")
    public boolean save(@RequestBody StudentVo studentVo){
        //先保存学生基本信息
        studentVo.setCreateTime(LocalDateTime.now());
        boolean result = iStudentService.saveOrUpdate(studentVo);
        //保存兴趣爱好的数据
        List<Integer> hobbyIdList = studentVo.getHobbyIdList();
        //删除关系表的数据
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("student_id",studentVo.getId());
        iSudentHobbyRelationService.remove(queryWrapper);
        //构建关系对象
        List<SudentHobbyRelation> relationList = new ArrayList<>();
        hobbyIdList.forEach(hobbyId->{
            SudentHobbyRelation sudentHobbyRelation = new SudentHobbyRelation(null,studentVo.getId(),hobbyId);
            relationList.add(sudentHobbyRelation);
        });
        //批量保存数据
        iSudentHobbyRelationService.saveBatch(relationList);
        return true;
    }

    /**
     * 根据Id查询回显信息
     * @param id
     * @return
     */
    @GetMapping("getStudentVoById")
    public StudentVo getStudentVoById(Integer id){
        StudentVo studentVo = new StudentVo();
        //查询学生基本信息
        Student student = iStudentService.getById(id);
        //使用工具类copy属性
        //org.springframework.beans
        BeanUtils.copyProperties(student,studentVo);
        //查询选中的兴趣爱好
        List<Integer> hobbyIdList = iSudentHobbyRelationService.getHobbyIdListByStuId(id);
        studentVo.setHobbyIdList(hobbyIdList);
        //三级联动的回显
        //查询市数据列表
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("pid", studentVo.getProviceId());
        List<Area> cityList = iAreaService.list(queryWrapper);
        studentVo.setCityList(cityList);
        //查询区数据列表
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("pid", studentVo.getCityId());
        List<Area> areaList = iAreaService.list(queryWrapper1);
        studentVo.setAreaList(areaList);
        return studentVo;
    }
}
